package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Body1
import com.abc.robot01.retrofit.bean.Condition3
import com.abc.robot01.retrofit.bean.Customer
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import com.abc.robot01.retrofit.bean.HouseType
import com.abc.robot01.retrofit.bean.Type
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch


class CustomersViewModel : ViewModel() {
    val TAG = "CustomersViewModel"
    val customerPhone: MutableState<String> = mutableStateOf("")
    val customerName: MutableState<String> = mutableStateOf("")


    //当前选择的客户状态 code
    val selectedStatusCode = mutableStateOf("")

    val selected20 = mutableStateOf("请选择")
    val width = mutableStateOf(15)
    val height = mutableStateOf(388)

    val selected10 = mutableStateOf("全部")
    val expanded1 = mutableStateOf(false)
    val show = mutableStateOf(false)

    val selectedIndex = mutableStateOf(-1)


    //客户状态
    private val _question5List = MutableStateFlow<List<HouseType>>(mutableListOf(HouseType()))
    val question5List: StateFlow<List<HouseType>> = _question5List


    //房屋类型
    val houseTypeList = mutableStateListOf<ButtonState>()


    //客户列表
    private val _customerList = MutableStateFlow<List<Customer>>(emptyList())
    val customerList: StateFlow<List<Customer>> = _customerList

    fun load() {
        request()
    }


    //以下是Item的信息
    //        ..当前选择的客户信息
    val selectedCustomerPhone: MutableState<String> = mutableStateOf("")
    val selectedCustomerName: MutableState<String> = mutableStateOf("")
    val selectedCustomerStatus: MutableState<String> = mutableStateOf("")

//修改之后的用户名称
    val newCustomerName: MutableState<String> = mutableStateOf("")

    //当前选择的客户状态 code
    val selectedItemStatusCode = mutableStateOf("")
    val selectedItemStatusValue = mutableStateOf("")

    suspend fun updateCustomer(
        customerName: String = selectedCustomerName.value,
        customerPhone: String = selectedCustomerPhone.value,
        newStatus: String = if(selectedItemStatusCode.value=="") selectedCustomerStatus.value else selectedItemStatusCode.value,
        newName: String = newCustomerName.value,
    ) {
       val a = RetrofitService.instance.updateCustomer(
                accessToken = Robot.accessToken, userId = Robot.userId, companyId = Robot.companyId,
                request = Body1(
                    customerName = customerName,
                    customerPhone = customerPhone,
                    updateCustomerStatus = newStatus,
                    updateCustomerName = newName
                )
            )
            request()
            Log.e(TAG, "updateCustomer:"+a)
    }

    fun  reset(){
        houseTypeList.forEach {
            it.isSelected = false
        }
    }

    

    fun init() {
        viewModelScope.launch {
            val s5 = RetrofitService.instance.getSysDicByType(
                accessToken = Robot.accessToken,
                type = "CUSTOMER_STATUS"
            )
            _question5List.value += s5.data
            _question5List.value.forEach {
                if (it.name != "全部") {
                    houseTypeList.add(
                        ButtonState(
                            label = it.name,
                            code = it.code,
                            isSelected = false
                        ).also {
//                           Log.e(TAG, "客户状态0  :${it}")
                        })
                }
            }
        }
        request()
    }

    private fun request(
    ) {
        Log.e(TAG, "当前选择的客户状态:${selectedStatusCode.value}")
        viewModelScope.launch {
            val res = RetrofitService.instance.getCustomers(
                accessToken = Robot.accessToken,
                userId = Robot.userId,
                companyId = Robot.companyId,
                request = FetchDataRequest9(
                    condition = Condition3(
                        customerName = customerName.value,
                        customerPhone = customerPhone.value,
                        customerStatus = selectedStatusCode.value,
                    )
                )
            )
            Log.e(TAG, "客户列表:$res")
            if (res.code == 200) {
                _customerList.value = res.data.list
            }
        }
    }
}
